﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Common
{
    /// <summary>
    /// 获取或设置一个对象的属性值
    /// </summary>
    public static class PropertyHelper
    {
        /// <summary>
        /// Sets the property.(设置一个字符串值)
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public static void SetProperty(object o, string name, string value)
        {
            object tempValue = null;
            Type objectType = o.GetType();
            PropertyInfo propertyInfo = objectType.GetProperty(name);
            Type propertyType = propertyInfo.PropertyType;
            string typeName = propertyType.Name;
            string fullName=propertyType.FullName;
            tempValue = GetValue(value, tempValue, typeName, fullName);
            
           
            if (propertyInfo != null && propertyInfo.CanWrite)
            {
                propertyInfo.SetValue(o, tempValue, null);
            }
        }

        private static object GetValue(string value, object tempValue, string typeName, string fullName)
        {
            typeName = typeName.ToLower();
            switch (typeName)
            {

                case "int32":
                    tempValue = TypeHelper.GetInt(value);
                    break;
                case "string":
                    tempValue = TypeHelper.GetString(value);
                    break;
                case "datetime":
                    tempValue = TypeHelper.GetDateTime(value);
                    break;
                case "nullable`1":
                    tempValue = GetNullable(fullName, value, tempValue);
                    break;
                case "decimal":
                    tempValue = TypeHelper.GetDec(value);
                    break;

                default:
                    throw new Exception("不识别的类型");



            }
            return tempValue;
        }

        private static object GetNullable(string p, string value,object tempValue)
        {
            string regex = "(?<=System.Nullable`1..System.).+?(?=,)";
            System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Multiline)
                        | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
           System.Text.RegularExpressions.Match m= reg.Match(p);
           string strName = m.Value;
           return GetValue(value, tempValue, strName, strName);
                
        }

        /// <summary>
        /// Gets the int.(从字符串转成int,忽略转换错误)
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private static object GetInt(string value)
        {
            object o = null;
            try
            {
                o = TypeHelper.GetInt(value);
            }
            catch (Exception)
            {


            }

            return o;
        }
        /// <summary>
        /// Sets the property.(设置一个对象值)
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public static void SetProperty(object o, string name, object value)
        {
            object tempValue = null;
            Type objectType = o.GetType();
            PropertyInfo propertyInfo = objectType.GetProperty(name);
            Type propertyType = propertyInfo.PropertyType;
            switch (propertyType.Name)
            {
                case "Int32":
                    tempValue = TypeHelper.GetInt(value);
                    break;
                case "String":
                    tempValue = TypeHelper.GetString(value);
                    break;
                case "Boolean":
                    tempValue = TypeHelper.GetBool(value);
                    break;
                default:
                    throw new Exception("不识别的类型");

            }


            if (propertyInfo != null && propertyInfo.CanWrite)
            {
                propertyInfo.SetValue(o, tempValue, null);
            }
        }


        /// <summary>
        /// Gets the property.(获取一个对象某个属性的值)
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        private static object GetProperty(object o, string name)
        {
            object tempValue = null;
            Type objectType = o.GetType();
            PropertyInfo propertyInfo = objectType.GetProperty(name);
            Type propertyType = propertyInfo.PropertyType;
            if (propertyInfo != null && propertyInfo.CanRead)
            {
                tempValue = propertyInfo.GetValue(o, null);
            }
            return tempValue;
            
        }
        /// <summary>
        /// Gets the string.(获取一个对象的某个属性的值,并转成字符串,确保该对象值是字符串)
        /// </summary>
        /// <param name="modelStudent">The model student.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetString(object modelStudent, string name)
        {
            string value = TypeHelper.GetString(GetProperty(modelStudent, name));
            return value;
        }
    }
}
